<template>
	<view>
		<view class="header">
			<view class="header-b">
				<view @click="gostock"><image src="../../static/images/arrow_left.png" mode=""></image></view>
				<view class="">{{ daty }}</view>
				<image src="../../static/stock/1.png" mode="" style="width:44rpx;height: 44rpx;"></image>
			</view>
		</view>
		<view class="seach">
			<view class="seach-s">
				<image src="../../static/images/tb_search.png" mode=""></image>
				<input type="text" value="" placeholder="请输入关键词..." @focus="add" />
			</view>
		</view>

		<view class="" v-for="(item, index) in any" :key="index">
			<!-- <view class="ima" v-if="shop"><image src="../../static/slice/86438f8f6d0029f503d57efb448590f.png" mode="aspectFit"></image></view> -->
			<view class="supershop">
				<image :src="item.cover" mode=""></image>
				<view class="supershop-sp">
					<view class="supershop-spa">{{ item.title }}(商品名称)</view>
					<view class="supershop-spa">
						库存: {{ kucun(item) }} (
						<span class="red">欠: {{ qian(item) }}</span>
						)
					</view>
				</view>
				<view class="supershop-top">
					<image src="../../static/stock/2.png" mode="" @click="del(index)"></image>

					<view class="blue" @click="showZhankai(item, index)">
						{{ item.show ? '关闭' : '展开' }}
						<image :src="image" mode=""></image>
					</view>
				</view>
			</view>
			<!-- 展开按钮功能 -->
			<view class="unfolt" v-show="item.show">
				<view class="unfolt-t">
					<view class="unfolt-one">规格</view>
					<view class="unfolt-two">欠货</view>
					<view class="unfolt-three">总库存</view>
				</view>
				<view class="unfolt-b" v-for="(child, sub) in item.product_list" :key="sub">
					<view class="unfolt-one">{{ child.spec_data[0] + '/' + child.spec_data[1] }}</view>
					<view class="unfolt-two">{{ child.owe_num }}</view>
					<view class="unfolt-three"><stepper style="float: right;" size="small" :min="-100" :max="100" :childindex="index" @change="changeNum"></stepper></view>
				</view>
			</view>
		</view>

		<view class="stock-b">
			<view class="stock-btn">
				<view class="stock-div">
					<view class="stock-d f28 c333">总入库数</view>
					<view class="stock-d f30 c666" >共{{ any.length }}种货品</view>
				</view>
				<view class="submit f30 cfff" @click="ruku">确认入库</view>
			</view>
		</view>
	</view>
</template>

<script>
import uniIcons from '@/components/uni-icons/uni-icons.vue';
export default {
	components: {
		uniIcons
	},

	data() {
		return {
			image: '../../static/images/arrow_down.png',
			show: true,
			showtru: true,
			shop: false,
			showZhan: '展开',
			showZhanimage: '../../static/images/tb_down.png',
			unfolt: false, //展开按钮,
			any: [],
			allIndex: '',
			operate_type: '',
			daty: '退货'
		};
	},
	onLoad() {
	
		
	},
	onShow() {
		this.any = uni.getStorageSync('kucun');
		let object = {};
		let objres = this.any.reduce((item, next) => {
			object[next.goods_id] ? '' : (object[next.goods_id] = true && item.push(next));
			return item;
		}, []);
		this.any = objres;
		// console.log(this.any);
		this.any.forEach(item => {
			item.show = false;
		});
		this.any.forEach(item => {
			item.product_list.forEach(item => {
				item.operate_num = 0;
			});
		});
		console.log(this.any);
	},
	methods: {
		showdow() {
			if (this.show) {
				this.show = false;
			} else {
				this.show = true;
			}
		},

		showtrue() {
			if (this.showtru) {
				this.showtru = false;
			} else {
				this.showtru = true;
			}
		},
		showZhankai(item, index) {
			if (item.show == true) {
				this.image = '../../static/images/arrow_down.png';
			} else {
				this.image = '../../static/images/arrow_ondown.png';
			}

			this.any[+index].show = !this.any[+index].show;
			this.$set(this.any, index, this.any[index]);
		},
		shhowdian() {
			if (this.showPut) {
				this.showPut = false;
			} else {
				this.showPut = true;
			}
		},
		changeNum(e) {
			console.log(e);
			this.any.forEach((item, i) => {
				if (item.show == true) {
					this.allIndex = i;
				}
			});
			this.any[+this.allIndex].product_list[e.childindex].operate_num = e.num;
		},
		add() {
			uni.navigateTo({
				url: 'Searchforgoods'
			});
		},
		kucun(e) {
			let b = 0;
			e.product_list.forEach(item => {
				b = +item.store_num + b;
			});
			return b;
		},
		qian(e) {
			let b = 0;
			e.product_list.forEach(item => {
				b = +item.owe_num + b;
			});
			return b;
		},
		del(e) {
			this.any.splice(e, 1);
		},
		ruku() {
			const c = [];
			this.any.forEach(item => {
				item.product_list.forEach(child => {
					// console.log(child);
					c.push(child);
				});
			});
			console.log(c);
			c.forEach(item => {
				delete item.spec_data;
				delete item.owe_num;
				delete item.trade_sum;
				delete item.sale_num;
				delete item.unit_id;
				delete item.unit_name;
				delete item.convert_num;
				delete item.store_num;
			});
			console.log(c);
			const e = {};
			e.order_id = 0;
			e.operate_type = this.operate_type;
			e.product_list = c;
			// console.log(e);
			this.$ajax('api/v1.stock/add', {
				0: JSON.stringify([e])
			})
				.then(res => {
					// console.log(res);
					if (res.code == 1) {
						uni.removeStorage({
							key: 'kucun'
						});
						uni.navigateTo({
							url: './stock'
						});
						if (this.operate_type == 1) {
							setTimeout(() => {
								uni.showToast({
									title: '入库成功',
									icon: 'none'
								});
							}, 1500);
						}
						if (this.operate_type == 2) {
							setTimeout(() => {
								uni.showToast({
									title: '出库成功',
									icon: 'none'
								});
							}, 1500);
						}
						if (this.operate_type == 3) {
							setTimeout(() => {
								uni.showToast({
									title: '调整成功',
									icon: 'none'
								});
							}, 1500);
						}
					}
				})
				.catch(res => {
					console.log(Error);
				});
		},
		gostock() {
			uni.removeStorage({
				key: 'kucun'
			});
			uni.navigateTo({
				url: './stock'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f5f5f5;
}
.header {
	width: 750rpx;
	padding-top: 50upx;
	background-color: #249AF6;
	.header-b {
		display: flex;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		justify-content: space-between;
		color: #ffffff;
		padding: 24rpx 31rpx;
		image {
			width: 21rpx;
			height: 38rpx;
		}
		.more {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
		}
	}

	.header-s {
		width: 143rpx;
		height: 34rpx;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
		line-height: 48rpx;
	}
}
.seach {
	width: 750rpx;
	background-color: #249AF6;
	padding-left: 32rpx;
	display: flex;
	padding-bottom: 25upx;

	.seach-s {
		width: 700rpx;
		height: 60rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 99999rpx;
		padding-left: 27rpx;
		display: flex;

		box-sizing: border-box;
		input {
			padding-top: 10rpx;
			border-style: none;
			font-size: 28upx;
			color: #666;
		}
		image {
			width: 31rpx;
			height: 28rpx;
			margin-top: 15rpx;
			margin-right: 20rpx;
		}
	}
	.seach-e {
		image {
			width: 44rpx;
			height: 44rpx;
			margin-top: 12rpx;
			margin-left: 10rpx;
		}
	}
}
.classify {
	display: flex;
	justify-content: space-around;
	font-size: 32rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: rgba(51, 51, 51, 1);
	height: 90rpx;
	line-height: 90rpx;
	background-color: #ffffff;
	border-bottom: 21rpx solid #f5f5f5;
	image {
		width: 17rpx;
		height: 10rpx;
		margin-left: 11rpx;
	}
}
.ima {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-top: 227rpx;
	image {
		width: 370upx;
		height: 390upx;
	}
}

.combobox {
	width: 100%;
	background-color: #ffffff;
	position: fixed;
	font-size: 30rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #333333;
	line-height: 48rpx;
	padding-left: 30rpx;
	z-index: 2;
	.show-li {
		height: 90rpx;
		line-height: 90rpx;
		border-bottom: 2rpx solid #eeeeee;
		image {
			width: 38rpx;
			height: 32rpx;
			margin: 50rpx;
			float: right;
		}
	}
}
.show {
	display: none;
}
.show-li:hover {
	color: #249af6;
}
.showtrue {
	display: none;
}
.supershop {
	height: 210rpx;
	background-color: #ffffff;
	padding: 40rpx 31rpx;
	line-height: 210rpx;
	box-sizing: border-box;
	display: flex;
	position: relative;
	border-bottom: 2rpx solid #cccccc;

	image {
		width: 131rpx;
		height: 131rpx;
	}

	.supershop-sp {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		line-height: 60rpx;
	}
	.supershop-top {
		line-height: 60rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		position: absolute;
		right: 50rpx;
		.put {
			width: 345rpx;
			height: 161rpx;
			position: absolute;
			left: -345rpx;
			background-color: blue;
			z-index: 2;
			padding-top: 34rpx;
			box-sizing: border-box;
			background-color: #ffffff;
			.put-top {
				display: flex;
				justify-content: space-evenly;
				image {
					width: 44rpx;
					height: 44rpx;
				}
			}
		}
		image {
			width: 49rpx;
			height: 48rpx;
			position: relative;
			top: -10rpx;
		}
		.blue {
			image {
				margin-left: 10rpx;
				width: 17rpx;
				height: 10rpx;
			}
		}
	}
}
.unfolt {
	height: 200rpx;
	background-color: #f5f5f5;
	font-size: 32rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: rgba(51, 51, 51, 1);

	.unfolt-t {
		.unfolt-three {
			position: relative;
			left: -50rpx;
		}
		padding: 0 40rpx;
		padding-right: 50rpx;
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;
		border-bottom: 2rpx solid #cccccc;
	}
	.unfolt-b {
		.unfolt-two {
			position: relative;
			left: 50rpx;
		}
		padding: 0 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 100rpx;
		line-height: 100rpx;
		border-bottom: 2rpx solid #cccccc;
	}
}
.stock-b {
	width: 100%;
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: $white-background;
}
.stock-btn {
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	.stock-div {
		width: 439rpx;
		height: 110rpx;
		background-color: #ffffff;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		.stock-d {
			margin-left: 32rpx;
			margin-top: 14rpx;
			&:last-child{
				margin-top: 3upx;
			}
		}
	}
	.submit{
		width: 260upx;
		height: 110upx;
		line-height: 110upx;
		text-align: center;
		background-color: #249AF6;
	}

	button {
		flex: 1;
	}
}
.red {
	color: red;
}
.blue {
	color: #249af6;
}
</style>
